<template>
  <div class="integral-list">
    <div class="weui-flex pl line-bottom" v-for="item,index in integralList">
      <div class="img">
        <img :src="item.mainPic?URL+item.mainPic:nopic" :width="!item.mainPic?50:60" />
      </div>
      <div class="weui-flex__item pd10">
        <div class="name">{{item.itemName}}<span class="price color-3 font-12">￥{{item.itemTruePrice}}</span></div>
        <div class="info font-12">{{item.needBonus}}积分</div>
      </div>
      <div class="cart">
        <div class="cart-input-number">
          <span class="cart-del" @click="inputDel(index)"></span>
          <span class="cart-input">{{item.itemAmount}}</span>
          <span class="cart-add" @click="inputAdd(index)"></span>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
  const config = require('~src/config')
  import { mapGetters } from 'vuex'
  export default {
    name: 'j-shopinfo',
    data () {
      return {
        URL: config._API_,
        nopic: config._URL_ + '/static/images/nopic.png',
        shopinfo: []
      }
    },
    computed: {
      ...mapGetters({
        integralList: 'getIntegralList'
      })
    },
    watch: {
      shopinfo (val) {
        console.log(val)
        this.$emit('on-shop-info', val)
      }
    },
    methods: {
      // 添加
      inputAdd (index) {
        var current = this.integralList[index]
        if (current.itemAmount >= current.itemNumber) {
          this.$store.dispatch('TOAST_SHOW', {content: '库存不够', msg: true})
        } else {
          current.itemAmount++
          if (current.itemAmount === 1) {
            this.shopinfo.splice(index, 0, {
              noGasItemId: current.id,
              itemName: current.itemName,
              needBonus: current.needBonus,
              itemNumber: current.itemAmount
            })
          } else {
            this.$set(this.shopinfo,index,{
              noGasItemId: current.id,
              itemName: current.itemName,
              needBonus: current.needBonus,
              itemNumber: current.itemAmount
            })
          }
        }
        this.intTotal = current.ItemAmount * current.itemNumber
      },
      // 删除
      inputDel (index) {
        var current = this.integralList[index]
        if(current.itemAmount === 1){
          this.shopinfo.splice(index, 1)
        }
        if (current.itemAmount <= 0) {
          this.$store.dispatch('TOAST_SHOW', {content: '数量不为能0', msg: true})
        } else {
          current.itemAmount--
          if (current.itemAmount >= 1 && this.shopinfo.length >0) {
            this.$set(this.shopinfo,index,{
              noGasItemId: current.id,
              itemName: current.itemName,
              needBonus: current.needBonus,
              itemNumber: current.itemAmount
            })
          }
        }
      }
    },
    created () {
    }
  }
</script>
<style lang="scss">

</style>
